﻿using DevelopAssistant.Service;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data;
using System.Text; 

namespace DevelopAssistant.Core.ToolBox
{
    public class AddSqlServerConnection : AddDataBaseConnection
    {
        private System.Windows.Forms.ComboBox combServer;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtPort;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtInstance;
        private System.Windows.Forms.CheckBox chkInstance;
        private System.Windows.Forms.GroupBox gboxLoginPanel;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.TextBox txtPassword;
        private System.Windows.Forms.TextBox txtUser_ID;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.CheckBox chkLoginStyle;
        private System.Windows.Forms.ComboBox combDataBases;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.CheckBox chkEfficient;
        private System.Windows.Forms.Label tester;
        private System.Windows.Forms.Label label1;

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AddSqlServerConnection));
            this.label1 = new System.Windows.Forms.Label();
            this.combServer = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtPort = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtInstance = new System.Windows.Forms.TextBox();
            this.chkInstance = new System.Windows.Forms.CheckBox();
            this.gboxLoginPanel = new System.Windows.Forms.GroupBox();
            this.label7 = new System.Windows.Forms.Label();
            this.txtPassword = new System.Windows.Forms.TextBox();
            this.txtUser_ID = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.chkLoginStyle = new System.Windows.Forms.CheckBox();
            this.combDataBases = new System.Windows.Forms.ComboBox();
            this.label6 = new System.Windows.Forms.Label();
            this.chkEfficient = new System.Windows.Forms.CheckBox();
            this.tester = new System.Windows.Forms.Label();
            this.gboxLoginPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.Location = new System.Drawing.Point(20, 59);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(77, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "服务器地址：";
            // 
            // combServer
            // 
            this.combServer.BackColor = System.Drawing.Color.White;
            this.combServer.FormattingEnabled = true;
            this.combServer.Items.AddRange(new object[] {
            "127.0.0.1"});
            this.combServer.Location = new System.Drawing.Point(92, 56);
            this.combServer.Name = "combServer";
            this.combServer.Size = new System.Drawing.Size(131, 20);
            this.combServer.TabIndex = 2;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.BackColor = System.Drawing.Color.Transparent;
            this.label2.Location = new System.Drawing.Point(233, 59);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 3;
            this.label2.Text = "端口：";
            // 
            // txtPort
            // 
            this.txtPort.BackColor = System.Drawing.Color.White;
            this.txtPort.Location = new System.Drawing.Point(271, 55);
            this.txtPort.Name = "txtPort";
            this.txtPort.Size = new System.Drawing.Size(63, 21);
            this.txtPort.TabIndex = 4;
            this.txtPort.Text = "默认";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.BackColor = System.Drawing.Color.Transparent;
            this.label3.Location = new System.Drawing.Point(56, 90);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 12);
            this.label3.TabIndex = 5;
            this.label3.Text = "实例：";
            // 
            // txtInstance
            // 
            this.txtInstance.BackColor = System.Drawing.SystemColors.Control;
            this.txtInstance.Enabled = false;
            this.txtInstance.Location = new System.Drawing.Point(92, 87);
            this.txtInstance.Name = "txtInstance";
            this.txtInstance.Size = new System.Drawing.Size(131, 21);
            this.txtInstance.TabIndex = 6;
            // 
            // chkInstance
            // 
            this.chkInstance.AutoSize = true;
            this.chkInstance.BackColor = System.Drawing.Color.Transparent;
            this.chkInstance.Location = new System.Drawing.Point(235, 89);
            this.chkInstance.Name = "chkInstance";
            this.chkInstance.Size = new System.Drawing.Size(48, 16);
            this.chkInstance.TabIndex = 7;
            this.chkInstance.Text = "实例";
            this.chkInstance.UseVisualStyleBackColor = false;
            this.chkInstance.Click += new System.EventHandler(this.chkInstance_Click);
            // 
            // gboxLoginPanel
            // 
            this.gboxLoginPanel.BackColor = System.Drawing.Color.Transparent;
            this.gboxLoginPanel.Controls.Add(this.label7);
            this.gboxLoginPanel.Controls.Add(this.txtPassword);
            this.gboxLoginPanel.Controls.Add(this.txtUser_ID);
            this.gboxLoginPanel.Controls.Add(this.label5);
            this.gboxLoginPanel.Controls.Add(this.label4);
            this.gboxLoginPanel.Enabled = false;
            this.gboxLoginPanel.Location = new System.Drawing.Point(22, 125);
            this.gboxLoginPanel.Name = "gboxLoginPanel";
            this.gboxLoginPanel.Size = new System.Drawing.Size(313, 107);
            this.gboxLoginPanel.TabIndex = 8;
            this.gboxLoginPanel.TabStop = false;
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(258, 37);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(41, 12);
            this.label7.TabIndex = 5;
            this.label7.Text = "如：sa";
            // 
            // txtPassword
            // 
            this.txtPassword.Location = new System.Drawing.Point(93, 71);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.PasswordChar = '*';
            this.txtPassword.Size = new System.Drawing.Size(159, 21);
            this.txtPassword.TabIndex = 4;
            // 
            // txtUser_ID
            // 
            this.txtUser_ID.Location = new System.Drawing.Point(93, 34);
            this.txtUser_ID.Name = "txtUser_ID";
            this.txtUser_ID.Size = new System.Drawing.Size(159, 21);
            this.txtUser_ID.TabIndex = 3;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(36, 74);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(41, 12);
            this.label5.TabIndex = 2;
            this.label5.Text = "密码：";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(34, 37);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(53, 12);
            this.label4.TabIndex = 1;
            this.label4.Text = "用户名：";
            // 
            // chkLoginStyle
            // 
            this.chkLoginStyle.AutoSize = true;
            this.chkLoginStyle.BackColor = System.Drawing.Color.Transparent;
            this.chkLoginStyle.Checked = true;
            this.chkLoginStyle.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkLoginStyle.Location = new System.Drawing.Point(55, 126);
            this.chkLoginStyle.Name = "chkLoginStyle";
            this.chkLoginStyle.Size = new System.Drawing.Size(156, 16);
            this.chkLoginStyle.TabIndex = 0;
            this.chkLoginStyle.Text = "Windows 集成身份登陆：";
            this.chkLoginStyle.UseVisualStyleBackColor = false;
            this.chkLoginStyle.Click += new System.EventHandler(this.chkLoginStyle_Click);
            // 
            // combDataBases
            // 
            this.combDataBases.BackColor = System.Drawing.SystemColors.Control;
            this.combDataBases.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.combDataBases.FormattingEnabled = true;
            this.combDataBases.Location = new System.Drawing.Point(115, 248);
            this.combDataBases.Name = "combDataBases";
            this.combDataBases.Size = new System.Drawing.Size(159, 20);
            this.combDataBases.TabIndex = 9;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.BackColor = System.Drawing.Color.Transparent;
            this.label6.Location = new System.Drawing.Point(32, 251);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(77, 12);
            this.label6.TabIndex = 10;
            this.label6.Text = "数据库名称：";
            // 
            // chkEfficient
            // 
            this.chkEfficient.AutoSize = true;
            this.chkEfficient.BackColor = System.Drawing.Color.Transparent;
            this.chkEfficient.Location = new System.Drawing.Point(115, 275);
            this.chkEfficient.Name = "chkEfficient";
            this.chkEfficient.Size = new System.Drawing.Size(96, 16);
            this.chkEfficient.TabIndex = 11;
            this.chkEfficient.Text = "高效连接模式";
            this.chkEfficient.UseVisualStyleBackColor = false;
            // 
            // tester
            // 
            this.tester.AutoSize = true;
            this.tester.BackColor = System.Drawing.Color.Transparent;
            this.tester.ForeColor = System.Drawing.Color.Blue;
            this.tester.Location = new System.Drawing.Point(221, 277);
            this.tester.Name = "tester";
            this.tester.Size = new System.Drawing.Size(53, 12);
            this.tester.TabIndex = 12;
            this.tester.Text = "测试连接";
            this.tester.Click += new System.EventHandler(this.tester_Click);
            // 
            // AddSqlServerConnection
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.ClientSize = new System.Drawing.Size(354, 366);
            this.Controls.Add(this.tester);
            this.Controls.Add(this.chkEfficient);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.combDataBases);
            this.Controls.Add(this.chkLoginStyle);
            this.Controls.Add(this.chkInstance);
            this.Controls.Add(this.txtInstance);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtPort);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.combServer);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.gboxLoginPanel);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
            this.MinimumSize = new System.Drawing.Size(187, 75);
            this.Name = "AddSqlServerConnection";
            this.Text = "Sql数据库连接";
            this.Controls.SetChildIndex(this.gboxLoginPanel, 0);
            this.Controls.SetChildIndex(this.label1, 0);
            this.Controls.SetChildIndex(this.combServer, 0);
            this.Controls.SetChildIndex(this.label2, 0);
            this.Controls.SetChildIndex(this.txtPort, 0);
            this.Controls.SetChildIndex(this.label3, 0);
            this.Controls.SetChildIndex(this.txtInstance, 0);
            this.Controls.SetChildIndex(this.chkInstance, 0);
            this.Controls.SetChildIndex(this.chkLoginStyle, 0);
            this.Controls.SetChildIndex(this.combDataBases, 0);
            this.Controls.SetChildIndex(this.label6, 0);
            this.Controls.SetChildIndex(this.chkEfficient, 0);
            this.Controls.SetChildIndex(this.tester, 0);
            this.gboxLoginPanel.ResumeLayout(false);
            this.gboxLoginPanel.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

    
        public AddSqlServerConnection()
        {
            InitializeComponent();
            InitializeControls();
        }

        public AddSqlServerConnection(MainForm OwnerForm)
            : this()
        {          
            this.Apply = new EventHandler(Apply_Click); 
        }

        private void InitializeControls()
        {
            SetBtnApplyEnabled(false);
            var serverslist = GetServersRecords("System.Data.Sql");
            combServer.Items.Clear();
            combServer.Items.AddRange(serverslist.ToArray());
            if (combServer.Items.Count > 0)
                combServer.SelectedIndex = 0;
        }

        private void Apply_Click(object sender, EventArgs e)
        {
            if(combDataBases.Items .Count <1)
            {
                MessageBox.Show("请选择要连接的数据库名");
                return;
            }

            string connectionString = "";
            connectionString += "Server=" + combServer.Text.ToString();
            if (txtPort.Text != "默认")
            {
                connectionString += ":" + txtPort.Text;
            }
            if (!string.IsNullOrEmpty(txtInstance.Text))
            {
                connectionString += "\\" + txtInstance.Text;
            }
            connectionString += ";";
            if (!string.IsNullOrEmpty(combDataBases.Text.ToString()))
            {
                connectionString += "Initial Catalog=" + combDataBases.Text.ToString() + ";";
            }
            else
            {
                connectionString += "Initial Catalog=" + "master;";
            }
            if (!chkLoginStyle.Checked)
            {
                connectionString += "User ID=" + txtUser_ID.Text+";";
                connectionString += "Pwd=" + txtPassword.Text + ";";
            }
            else
            {
                connectionString += "Integrated Security=SSPI;";
            }

            ProviderName = "System.Data.SQL";
            ConnectionString = connectionString.Clone().ToString();
            AllObjectLoad = !chkEfficient.Checked;

        }
       
        private void tester_Click(object sender, EventArgs e)
        {
            combDataBases.Items.Clear();

            try
            {
                DataBaseServer dbserver = new DataBaseServer();
                dbserver.Server = combServer.SelectedText;
                string connectionString = "";
                connectionString += "Server=" + combServer.Text.ToString();
                if (txtPort.Text != "默认")
                {
                    connectionString += ":" + txtPort.Text;
                }
                if (!string.IsNullOrEmpty(txtInstance.Text))
                {
                    connectionString += "\\" + txtInstance.Text;
                }
                connectionString += ";";
                if (!string.IsNullOrEmpty(combDataBases.Text))
                {
                    connectionString += "Initial Catalog=" + combDataBases.Text + ";";
                }
                else
                {
                    connectionString += "Initial Catalog=" + "master;";
                }
                if (!chkLoginStyle.Checked)
                {
                    connectionString += "User ID=" + txtUser_ID.Text+";";
                    connectionString += "Pwd=" + txtPassword.Text + ";";
                }
                else
                {
                    connectionString += "Integrated Security=SSPI;";
                }

                ProviderName = "System.Data.SQL";
                ConnectionString = connectionString.Clone().ToString();
                AllObjectLoad = chkEfficient.Checked;

                dbserver.ConnectionString = ConnectionString;
                dbserver.ProviderName = ProviderName;

                using (var db = DevelopAssistant.Service.Utility.GetAdohelper(dbserver))
                {
                    string SqlText = "select name from sysdatabases order by name";
                    DataTable dt = db.GetDataSet(System.Data.CommandType.Text, SqlText, null).Tables[0];

                    foreach (DataRow dr in dt.Rows)
                    {
                        string databaseName = dr["name"] + "";
                        combDataBases.Items.Add(databaseName.Trim());
                    }

                    if (combDataBases.Items.Count > 0)
                        combDataBases.SelectedIndex = 0;

                }

                SetBtnApplyEnabled(true);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                SetBtnApplyEnabled(false);
            }           

        }

        private void chkInstance_Click(object sender, EventArgs e)
        {
            if (chkInstance.Checked)
            {
                txtInstance.Enabled = true;
                txtInstance.BackColor = System.Drawing.Color.White;
            }
            else
            {
                txtInstance.Enabled = false;
                txtInstance.BackColor = System.Drawing .SystemColors.Control;
            }
        }

        private void chkLoginStyle_Click(object sender, EventArgs e)
        {
            if (chkLoginStyle.Checked)
            {
                gboxLoginPanel.Enabled = false;
            }
            else
            {
                gboxLoginPanel.Enabled = true;
            }
        }

    }
}
